<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            离线上传
        </div>
    </header>

    <div class="aui-content aui-margin-b-15" style="margin-top:50px;"></div>
    <div class="aui-btn aui-btn-info" style='margin-top:5%;margin-left:8%;width:84%;' onclick="upLoad()">上传离线数据</div>
    <ul class="aui-list aui-form-list" style='margin-top:2%;'>
        <li class="aui-list-item">
            <div class="aui-list-item-inner">
                <div class="aui-list-item-label">盘点单号</div>
                <div class="aui-list-item-input">
                    <input type="text" placeholder="输入/选择盘点单号" id="billCode" value="" onclick="getIdSelector();" />
                </div>
                <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWO()"></i>
            </div>
        </li>
    </ul>
    </div>
    <p></p>
    <p></p>
    <div style="width:100%;margin-top:2%;"></div>
    <div class="aui-btn aui-btn-info" style="width:42%;margin-left:5%;" onclick="doSearch()">查询本地数据库</div>
    <div class="aui-btn aui-btn-info" style="margin-left:5%;width:42%;" onclick="deleteData()">删除离线数据</div>
    
    <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;margin-bottom:50px;">
        <table class="table" style='width:100%;' id="table">
            <thead>
                <tr
                    style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 12px;font-weight: bold;">
                    <td algin='center' width='20%'></td>
                    <td algin='center' width='80%'>器具条码</td>
                </tr>
            </thead>
            <tbody id="tbodyHtml">
            </tbody>
        </table>
    </div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">

    var sysid = ""; //盘点单据GID
    var billCode = "";//盘点单号

    function upLoad() {
        //盘点单号
        var billCode = $("#billCode").val();
        //盘点单号非空校验
        if (billCode == "" || billCode == null || billCode == undefined) {
            api.toast({
                msg: '盘点单号不能为空',
                duration: 3000
            });
            return;
        }
        var db = api.require('db');
        db.openDatabase({
            name: 'test'
        }, function (ret, err) {
            db.selectSql({
                name: 'test',
                sql: "SELECT * FROM AssetEncoding "
            }, function (ret, err) {
                if (ret.status) {
                    if (ret.status) {
                        var un = localStorage.getItem('un');
                        var serverUrl = localStorage.getItem('serverUrl');
                        var url = serverUrl + "/app/upLoadData";
                        var aa = JSON.stringify(ret);
                        var myobj = eval("(" + aa + ")");


                        const qjCode = myobj.data;
                        const arrayOfObjects = JSON.parse(JSON.stringify(qjCode));
                        var codeBar = "";
                        arrayOfObjects.forEach((obj, index) => {
                            codeBar += (obj.qjCode + ',');
                        });

                        if (codeBar == "" || codeBar == null || codeBar == undefined) {
                            api.toast({
                                msg: '没有需要上传的数据',
                                duration: 3000
                            });
                            return;
                        }else{
                            codeBar = codeBar.substring(0,codeBar.length - 1)
                        }

                        var data = {
                            values: { ////以表单方式提交参数（JSON对象）
                                codeBar: codeBar,
                                billCode: billCode,
                                appVersion: api.appVersion,
                                un: un
                            }
                        };
                        api.ajax({
                            url: url,
                            method: 'post',
                            dataType: 'json',
                            timeout: 30,
                            returnAll: false,
                            data: data
                        }, function (ret, err) {
                            if (ret) {
                                var data = JSON.stringify(ret);
                                var myobj = eval("(" + data + ")");
                                //成功
                                if (myobj.code == '0') {

                                    api.hideProgress();
                                    api.toast({
                                        msg: "保存成功",
                                        duration: 3000
                                    });

                                    // //清空表单
                                    // $("#billCode").val("");
                                    var db = api.require('db');
                                    db.openDatabase({
                                        name: 'test'
                                    }, function (ret, err) {
                                        db.selectSql({
                                            name: 'test',
                                            // sql: "SELECT * FROM AssetEncoding where billCode = '" + billCode + "'"
                                            //   sql: "DELETE FROM AssetEncoding WHERE billCode = '" + billCode + "'"
                                            sql: "DELETE FROM AssetEncoding"
                                        }, function (ret, err) {
                                            if (ret.status) {
                                                //    alert(JSON.stringify(ret));
                                                console.log(JSON.stringify(ret));
                                            } else {
                                                alert(JSON.stringify(err));
                                            }
                                        });

                                    });
                                    doSearch()
                                } else if (myobj.code == '1') {
                                    api.hideProgress();
                                    api.toast({
                                        msg: myobj.message,
                                        duration: 3000
                                    });
                                }
                            } else {
                                api.hideProgress();
                                alert(JSON.stringify(err));
                            }
                        });
                    } else {
                        api.hideProgress();
                        alert(JSON.stringify(err));
                    }
                }
            });
        });
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        if (el.id == "rsCode") {
            $("#rsCode").select();
        }
        idSelector = el.id;
        $("#" + el.id).select();
    }
    //选择盘点单号
    function goSelectWO() {
        api.openWin({
            name: 'rsInventoryPddh',
            url: 'rsInventoryPddh.html',
            pageParam: {
                name: "rsInventory"
            }
        });
    }
    //盘点单号选择后回调函数
    function setBillCode(sysid, billcode) {
        sysid = sysid;
        billcode = billcode;
        document.getElementById('billCode').value = billcode;

    }

    //查询本地可上传盘点单号
    function doSearch() {
        //工单号
        var billCode = $("#billCode").val();
        var sql = ""

        sql = "SELECT qjCode FROM AssetEncoding group by qjCode"

        var db = api.require('db');
        db.openDatabase({
            name: 'test'
        }, function (ret, err) {
            db.selectSql({
                name: 'test',
                sql: sql
            }, function (ret, err) {
                if (ret.status) {
                
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                const qjCode = myobj.data;
                console.log(qjCode);
                var html = "";
                const arrayOfObjects = JSON.parse(JSON.stringify(qjCode));

                arrayOfObjects.forEach((obj, index) => {
                    console.log(`第 ${index + 1}条数据`);
                    console.log(`qjCode: ${obj.qjCode}`);
                    html += "<tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;'>";
                    html += "<td style='width:40px;'><input name='sub' type='checkbox' /></td>";
                    html += `<td name='qjCode'>${obj.qjCode}</td>`;
                    html += "</tr>";
                });

                // Object.values(JSON.stringify(qjCode)).forEach(value => {
                //     console.log(value);
                //     html += "<tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;'>";
                //     html += "<td style='width:40px;'><input name='sub' type='checkbox' /></td>";
                //     html += "<td name='qjCode'>" + value + "</td>";
                //     html += "</tr>";
                // });


                    $api.html($api.byId("tbodyHtml"), html);

                } else {
                    api.hideProgress();
                    alert(JSON.stringify(err));
                }
            });
        });



    }
    function deleteData() {
        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        check.each(function () {
            count++;
        });

        //选中行不为1
        if (count == 0) {
            api.toast({
                msg: '请选择数据！',
                duration: 3000
            });
            return;
        }
        check.each(function () { //遍历
            var row = $(this).parent("td").parent("tr");
            var qjCode = row.find("[name='qjCode']").html(); //注意html()和val()

            var db = api.require('db');
            db.openDatabase({
                name: 'test'
            }, function (ret, err) {
                db.selectSql({
                    name: 'test',
                    sql: "DELETE FROM AssetEncoding WHERE qjCode = '" + qjCode + "'"
                }, function (ret, err) {
                    if (ret.status) {
                        console.log(JSON.stringify(ret));
                    } else {
                        alert(JSON.stringify(err));
                    }
                });
                api.hideProgress();
                api.toast({
                    msg: "本地数据已删除",
                    duration: 3000
                });

            });

        });
        doSearch();
    }
</script>

</html>